/*
 * JGO2RDF creates a RDF representation of GAF2 files, which are automatically sourced from GO ftp site.
 * Copyright (C) 2012 Matthew M. Hindle & Artem Lysenko
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package uk.co.jgo2rdf.goa.utils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


/**
 * @param <K>
 * @author lysenkoa
 */
public class FrequencyMapBuilder<K extends Object> {
    private Map<K, Integer> map;

    public FrequencyMapBuilder(Map<K, Integer> map) {
        this.map = map;
    }

    public void setMap(Map<K, Integer> map) {
        this.map = map;
    }

    public void addEntry(K k) {
        Integer n = map.get(k);
        if (n == null) {
            n = 0;
        }
        map.put(k, ++n);
    }

    public Map<K, Integer> getFrequencyCounts() {
        return map;
    }

    public Map<Integer, List<K>> getReverseMap() {
        CategoryMapBuilder<Integer, K> cat = new CategoryMapBuilder<Integer, K>(new HashMap<Integer, List<K>>());
        for (Entry<K, Integer> ent : map.entrySet())
            cat.addEntry(ent.getValue(), ent.getKey());
        return cat.getCategoryMap();
    }

    public void clear() {
        map.clear();
    }
}
